/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__resp_parse.c                                    */
/*  NOTE      =                                                    */
/*  DATE      = 2016/08/15 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <dubbo/dubbo_core.h>
#include "frsutl.h"
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frreport_comm.h"
#include "frbyte_buffer.h"
#include "../../../../src/module/dubbo/config.h"

extern int __resp_parse(Frbyte_Packet *p_packet, L7session_dubbo_private *p_st_private, Frreport_body_data *p_st_body_data, Conntrack *p_st_conn);
extern int dubbo_session_destroy(void *p_private_session);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_packet = NULL;
    L7session_dubbo_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    i_ret = __resp_parse(p_packet, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    return;
}

static void case2(void)
{
    int i_ret = 0;
    char *p_data = NULL;
    Frbyte_Packet *p_packet = NULL;
    L7session_dubbo_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = (char *)"\xda\xbb\x02\x14\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x39\x91\x30\x36\x48\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64\x30\x2c\x20\x72\x65\x73\x70\x6f\x6e\x73\x65\x20\x66\x6f\x72\x6d\x20\x70\x72\x6f\x76\x69\x64\x65\x72\x3a\x20\x31\x30\x2e\x31\x2e\x31\x30\x2e\x37\x38\x3a\x32\x30\x38\x38\x30";
    p_packet = bytebuff_creat();
    if(p_packet == NULL)
        goto ERROR;
    if(bytebuff_add(p_packet, p_data, 73) != FR_SUCC)
        goto ERROR;

    p_st_private = (L7session_dubbo_private *)frmalloc(sizeof(L7session_dubbo_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_dubbo_private));

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __resp_parse(p_packet, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));

    bytebuff_free(&p_packet);
    dubbo_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);

    return;

ERROR:
    if(p_packet)
        bytebuff_free(&p_packet);
    if(p_st_private)
        dubbo_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);

    return;
}

static void case3(void)
{
    int i_ret = 0;
    char *p_data = NULL;
    Frbyte_Packet *p_packet = NULL;
    L7session_dubbo_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    p_data = (char *)"\xda\xbb\x02\x14\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x39\x91\x30\x36\x48\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64\x30\x2c\x20\x72\x65\x73\x70\x6f\x6e\x73\x65\x20\x66\x6f\x72\x6d\x20\x70\x72\x6f\x76\x69\x64\x65\x72\x3a\x20\x31\x30\x2e\x31\x2e\x31\x30\x2e\x37\x38";
    p_packet = bytebuff_creat();
    if(p_packet == NULL)
        goto ERROR;
    if(bytebuff_add(p_packet, p_data, 67) != FR_SUCC)
        goto ERROR;

    p_st_private = (L7session_dubbo_private *)frmalloc(sizeof(L7session_dubbo_private));
    if(p_st_private == NULL)
        goto ERROR;
    memset(p_st_private, 0x00, sizeof(L7session_dubbo_private));

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    if(p_st_body_data == NULL)
        goto ERROR;
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    i_ret = __resp_parse(p_packet, p_st_private, p_st_body_data, p_st_conn);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC));

    bytebuff_free(&p_packet);
    dubbo_session_destroy(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_st_conn);

    return;

ERROR:
    if(p_packet)
        bytebuff_free(&p_packet);
    if(p_st_private)
        dubbo_session_destroy(p_st_private);
    if(p_st_body_data)
        frreport_body_free(&p_st_body_data);
    if(p_st_conn)
        frfree(p_st_conn);

    return;
}

int add_suite__resp_parse(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __resp_parse()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
